package com.redball;

import android.graphics.Canvas;
import android.graphics.Paint;

public class Wall
{

	public final int NONE = 0;
	public final int LOWER = 1;
	public final int UPPER = 2;
	public final int LEFT = 3;
	public final int RIGHT = 4;

	private Line upper;
	private Line left;
	private Line bottom;
	private Line right;

	public Wall()
	{
		upper = new Line(0, 0, 0, 0);
		right = new Line(0, 0, 0, 0);
		bottom = new Line(0, 0, 0, 0);
		left = new Line(0, 0, 0, 0);
	}
	
	public Wall(int Xa, int Ya, int Xb, int Yb, int Xc, int Yc, int Xd, int Yd)
	{
		
		upper = new Line(Xa, Ya, Xb, Yb);
		right = new Line(Xb, Yb, Xc, Yc);
		bottom = new Line(Xc, Yc, Xd, Yd);
		left = new Line(Xd, Yd, Xa, Ya);
	}
	
	public void setWall(int Xa, int Ya, int Xb, int Yb, int Xc, int Yc, int Xd, int Yd)
	{
		
		upper = new Line(Xa, Ya, Xb, Yb);
		right = new Line(Xb, Yb, Xc, Yc);
		bottom = new Line(Xc, Yc, Xd, Yd);
		left = new Line(Xd, Yd, Xa, Ya);
	}

	/**
	 * @return normal of the lower wall
	 */
	public double getNormal(Line line)
	{
		int dX = line.getEndX() - line.getStartX();
		int dY = line.getEndY() - line.getStartY();

		double normal = -Math.atan2(dY, dX) + Math.PI / 2.0;
		return normal;
	}
	

	public Line checkIfHit(int x, int y, int radius)
	{
//		System.out.println("UP ->");
		if (isCrossing(x, y, radius, upper))
			return upper;
//		System.out.println("BOTTOM ->");
		if (isCrossing(x, y, radius, bottom))
			return bottom;
//		System.out.println("RIGHT ->");
		if (isCrossing(x, y, radius, right))
			return right;
//		System.out.println("LEFT ->");
		if (isCrossing(x, y, radius, left))
			return left;
		return null;
	}

	private boolean isCrossing(int x, int y, int radius, Line line)
	{
		// Checking the ball is in the line's zone.
		int leftX, rightX, upY, downY;
		if (line.getEndX() > line.getStartX())
		{
			leftX = line.getStartX();
			rightX = line.getEndX();
		} else
		{
			rightX = line.getStartX();
			leftX = line.getEndX();
		}

		if (line.getEndY() > line.getStartY())
		{
			upY = line.getStartY();
			downY = line.getEndY();
		} else
		{
			downY = line.getStartY();
			upY = line.getEndY();
		}

		x += radius;
		y += radius;

		double dX = line.getEndX() - line.getStartX();
		double dY = line.getEndY() - line.getStartY();

		// Checking if the wall is straight
		if (dX != 0 && dY != 0)
		{
			double c = line.getStartY() - (dY/dX)*line.getStartX();
			double cnst = -dX * c;
			double dist = Math.abs(-dY * x + dX * y + cnst); 
					dist = dist / Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));

//			System.out.println("<@@@@@@@@@@@@@@@@@@@>");
//			System.out.println("ball X: " + x);
//			System.out.println("ball Y: " + y);
//			System.out.println("dX: " + dX);
//			System.out.println("dY: " + dY);
//			System.out.println("cnst:" + cnst);
//			System.out.println("c:" + c);
//			System.out.println("Radius: " + radius);
//			System.out.println("dist: " + dist);

			if ((Math.abs(dist) <= radius) && (x >= leftX) && (x <= rightX) && (y <= downY) && (y >= upY))
			{
//				System.out.println("<===========>");
//				System.out.println("HIT!!");
				return true;
			}
		} else if(dX == 0)
		{// Straight wall
//			System.out.println("<@@@@@@@@@@@@@@@@@@@>");
//			System.out.println("ball X: " + x);
//			System.out.println("ball Y: " + y);
//			System.out.println("dist: " +  (x - line.getEndX()));
//			System.out.println("dX: " + dX);
//			System.out.println("dY: " + dY);
			if (!((y <= downY) && (y >= upY)))
			{
				return false;
			}

			if (Math.abs(x - line.getStartX()) <= radius)
			{
//				System.out.println("<$$$$$$$$$$>");
//				System.out.println("SIDE HIT!!");
				return true;
			}
		}else if(dY == 0)
		{
//			System.out.println("<@@@@@@@@@@@@@@@@@@@>");
//			System.out.println("ball X: " + x);
//			System.out.println("ball Y: " + y);
//			System.out.println("dist: " +  (y - line.getStartY()));
//			System.out.println("dX: " + dX);
//			System.out.println("dY: " + dY);
			if (!((x >= leftX) && (x <= rightX)))
			{
				return false;
			}

			if (Math.abs(y - line.getStartY()) <= radius)
			{
//				System.out.println("<$$$$$$$$$$>");
//				System.out.println("UP / DOWN HIT!!");
				return true;
			}
		}

		return false;
	}

	/**
	 * Draws the wall
	 * 
	 * @param canvas
	 * @param p
	 *            the color of the wall.
	 */
	public void draw(Canvas canvas, Paint p)
	{
		drawLine(upper, p, canvas);
		drawLine(right, p, canvas);
		drawLine(bottom, p, canvas);
		drawLine(left, p, canvas);
	}

	private void drawLine(Line line, Paint p, Canvas canvas)
	{
		canvas.drawLine(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY(), p);
	}

	public String printLine(Line line)
	{
		if (line == upper)
			return "upper";
		if (line == bottom)
			return "bottom";
		if (line == right)
			return "right";
		if (line == left)
			return "left";
		return "";
	}
}
